<template>
  <el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible"
             :close-on-click-modal="false" draggable>
    <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
      <el-row :gutter="24">
        <el-col :span="12" class="mb20">
          <el-form-item label="所属板块id" prop="panelId">
<!--            <el-input v-model="form.panelId" placeholder="请输入所属板块id"/>-->
            <el-tree-select
                v-model="form.panelId"
                :data="form.parentData"
                :render-after-expand="false"
                :props="{ value: 'id', label: 'name', children: 'children' }"
                class="w100"
                clearable
                check-strictly
                :placeholder="$t('sysmenu.inputParentIdTip')"
            >
            </el-tree-select>
          </el-form-item>
        </el-col>
        <!--        0关联商品 1其他链接-->
        <el-col :span="12" class="mb20">
          <el-form-item label="类型" prop="type">
            <el-select v-model="form.type" placeholder="请选择">
              <el-option label="关联商品" :value=0></el-option>
              <el-option label="其他链接" :value=1></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="关联商品id" prop="productId">
            <el-input v-model="form.productId" placeholder="请输入关联商品id"/>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="排序" prop="sortOrder">
            <el-input-number v-model="form.sortOrder" :min="1" controls-position="right"/>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="其他链接" prop="fullUrl">
            <el-input v-model="form.fullUrl" placeholder="请输入其他链接"/>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label=" picUrl" prop="picUrl">
            <el-input v-model="form.picUrl" placeholder="请输入 picUrl"/>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="3d轮播图背景2" prop="picUrl2">
            <el-input v-model="form.picUrl2" placeholder="请输入3d轮播图背景2"/>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="3d轮播图背景3" prop="picUrl3">
            <el-input v-model="form.picUrl3" placeholder="请输入3d轮播图背景3"/>
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>
    <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false">取消</el-button>
          <el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
        </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="MallPanelContentDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from "/@/hooks/message";
import { getObj, addObj, putObj } from '/@/api/zhaikucloud/mallPanelContent'
import {fetchPanelTree} from '/@/api/zhaikucloud/mallpanel'
import { rule } from '/@/utils/validate';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false)
const loading = ref(false)
// 定义字典

// 提交表单数据
const form = reactive({
  id:'',
  panelId: '',
  type: '',
  productId: '',
  sortOrder: '',
  fullUrl: '',
  picUrl: '',
  picUrl2: '',
  picUrl3: '',
  parentData: [] as any[], // 上级菜单数据
});

// 定义校验规则
const dataRules = ref({
  panelId: [{required: true, message: '所属板块id不能为空', trigger: 'blur'}],
  type: [{required: true, message: '请选择类型', trigger: 'blur'}],
  productId: [{required: true, message: '关联商品id不能为空', trigger: 'blur'}],
  sortOrder: [{required: true, message: '不能为空', trigger: 'blur'}],
  fullUrl: [{required: true, message: '其他链接不能为空', trigger: 'blur'}],
  picUrl: [{required: true, message: '不能为空', trigger: 'blur'}],
  picUrl2: [{required: true, message: '3d轮播图背景2不能为空', trigger: 'blur'}],
  picUrl3: [{required: true, message: '3d轮播图背景3不能为空', trigger: 'blur'}],
})

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true
  form.id = ''

  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });

  // 获取mallPanelContent信息
  if (id) {
    form.id = id
    getmallPanelContentData(id)
  }
  // 渲染上级菜单列表树
  getAllMenuData();
};

// 提交
const onSubmit = async () => {
  const valid = await dataFormRef.value.validate().catch(() => {});
  if (!valid) return false;

  try {
    loading.value = true;
    form.id ? await putObj(form) : await addObj(form);
    useMessage().success(form.id ? '修改成功' : '添加成功');
    visible.value = false;
    emit('refresh');
  } catch (err: any) {
    useMessage().error(err.msg);
  } finally {
    loading.value = false;
  }
};

// 从后端获取菜单信息（含层级）
const getAllMenuData = () => {
  form.parentData = [];
  fetchPanelTree({
    lazy: Object,
    type: '0',
  }).then((res) => {
    let menu = {
      id: '1',
      name: '根菜单',
      children: [],
    };
    menu.children = res.data;
    form.parentData.push(menu);
  });
};


// 初始化表单数据
const getmallPanelContentData = (id: string) => {
  // 获取数据
  loading.value = true
  getObj(id).then((res: any) => {
    Object.assign(form, res.data)
  }).finally(() => {
    loading.value = false
  })
};

// 暴露变量
defineExpose({
  openDialog
});
</script>